/**=========================================================
 * Controller: SystemDictController
  =========================================================*/
App.controller('SystemDictController', ['$scope', 'toaster', 'ngDialog', 'Utils', 'HttpUtils', 'DialogUtils', 'SimpleUiGrid',
    function($scope, toaster, ngDialog, utils, httpUtils, dialogUtils, simpleUiGridService) {
        //===========================dictGrid=================================            
        var dictGridColumns = [
            {
                displayName: '字典代码',
                field: 'code'
            },
            {
                displayName: '字典名称',
                field: 'name'
            },
            {
                displayName: '排序',
                field: 'order_priority'
            },
            {
                displayName: '状态',
                field: 'status'
            },
            {
                displayName: '备注',
                field: 'remark'
            },
            { 
                name: '', 
                field: 'widgets', 
                width: '100',
                enableCellEdit: false,
                cellTemplate: '\
                    <div class="ui-grid-cell-contents" style="text-align:center;">\
                        <button ng-click="grid.appScope.addChildDict(row)" title="增加子字典" class="btn btn-xs btn-warning">\
                            <em class="fa fa-plus"></em>\
                        </button>\
                        <button ng-click="grid.appScope.saveDict(row.entity)" title="保存" class="btn btn-xs btn-info">\
                            <em class="fa fa-save"></em>\
                        </button>\
                        <button ng-click="grid.appScope.deleteDict(row.entity)" title="删除" class="btn btn-xs btn-danger">\
                            <em class="fa fa-trash"></em>\
                        </button>\
                    </div>\
                '
            }
        ];
        
        $scope.queryParams = {namespace: 'SystemMapper.getSysDicts'};
        $scope.searchDictGrid = function() {
            dictGrid.draw($scope.queryParams);
        }; 

        var dictGrid = simpleUiGridService.init($scope, 
            {
                optionModel: 'dictGridOptions',
                pageOptionModel: 'dictGridPageOptions',
                gridApi: 'dictGridApi',
                pageSize: 25,
                pageType: 'common',
                columns: dictGridColumns,
                needExport: false,
                expandable: true,
                controllerSearchFn: $scope.searchDictGrid,
                registerApiFn: function(gridApi){
                    gridApi.treeBase.on.rowExpanded($scope, function(row) {
                        if (!row.entity.$$childrenLoaded){
                            var currentLevel = row.entity.$$treeLevel + 1;
                            httpUtils.get(utils.getQueryListUrl(),
                                {
                                    namespace: 'SystemMapper.getSysDicts',
                                    parentID: row.entity.tid
                                },
                                function(result){
                                    if (!utils.isEmpty(result.datas.gridDatas)){
                                        var index
                                        for (var i = 0; i < $scope.dictGridOptions.data.length; i++){
                                            if (row.entity.$$hashKey === $scope.dictGridOptions.data[i].$$hashKey){
                                                index = i + 1;
                                                break;
                                            }
                                        }
                                        for (var j = 0; j < result.datas.gridDatas.length; j++){
                                            result.datas.gridDatas[j].$$treeLevel = currentLevel;
                                            $scope.dictGridOptions.data.splice(index + j, 0, result.datas.gridDatas[j]);
                                        }
                                        row.entity.$$childrenLoaded = true
                                    }
                                }
                            );
                        }
                    });
                },
                menus: [
                    {
                        title: '新增',
                        action: function ($event) {
                            dictGrid.addRow({status: '1', $$treeLevel: 0, $$childrenLoaded: false},0);
                        }
                    }
                ]
            }
        );
              
        $scope.addChildDict = function(row){
            var index = dictGrid.getIndex(row.entity) + 1;
            var treeLevel = row.entity.$$treeLevel + 1;
            dictGrid.addRow({
                parent_id: row.entity.tid,
                status: '1',
                $$treeLevel: treeLevel,
                $$childrenLoaded: false
            },index);
            dictGrid.expandRow(row);
        }
              
        $scope.saveDict = function(data){
            httpUtils.post('/sysDict/save',data,
                function(result){
                    if (result.success){
                        if (utils.isEmpty(data.tid)){
                            data.tid = result.msg;
                        }
                        toaster.pop('success', '', '保存成功');
                    }else{
                        toaster.pop('error', '', result.msg);
                    }
                },
                function(){
                    toaster.pop('error', '', '保存失败');
                }
            );
        };
        
        $scope.deleteDict = function(data){
            dialogUtils.confirm('确定要删除吗?',
                function () {
                    httpUtils.post('/sysDict/delete',data,
                        function(result){
                            if (result.success){
                                dictGrid.deleteRow(data);
                                toaster.pop('success', '', '删除成功');
                            }else{
                                toaster.pop('error', '', result.msg);
                            }
                        },
                        function(result){
                            toaster.pop('error', '', '删除失败');
                        }
                    );
                }
            );       
        };

        
//===========================init================================= 
        $scope.searchDictGrid();
    }
]);
